package com.estronger.shopmanage.ui.grab.adapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.ui.grab.bean.GrabOrderBean;
import com.estronger.shopmanage.ui.install.BuildDetailActivity;
import com.estronger.shopmanage.ui.measure.MeasureDetailActivity;
import com.estronger.shopmanage.ui.othertask.activity.OtherTaskDetailActivity;
import com.estronger.shopmanage.ui.othertask.bean.OtherTaskMainBean;
import com.estronger.shopmanage.ui.send.SendDetailActivity;
import com.google.gson.Gson;
import com.soonfor.evaluate.base.EvalBaseAdapter;
import com.soonfor.evaluate.tools.CommonUtils;
import com.soonfor.evaluate.tools.NoDoubleClickUtils;
import com.soonfor.evaluate.tools.Tokens;

import java.util.List;

/**
 * Created by Administrator on 2018-01-30.
 */

/**
 * 修改人：DC-ZhuSuiBo on 2018/2/2 14:39
 * 邮箱：suibozhu@139.com
 */
public class GrabOrderListAdapter extends EvalBaseAdapter<GrabOrderListAdapter.ViewHolder, GrabOrderBean.DataBean> implements View.OnClickListener {

    private List<GrabOrderBean.DataBean> list;

    Activity activity;
    private View.OnClickListener grabListener;

    public GrabOrderListAdapter(Activity context, List<GrabOrderBean.DataBean> list, View.OnClickListener grabListener) {
        super(context);
        activity = context;
        this.list = list;
        this.grabListener = grabListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.adapter_graborder, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        GrabOrderBean.DataBean gpBean = list.get(position);
        holder.setData(gpBean);
        setViewByStatus(gpBean, holder);
        holder.llfItem.setTag(position);
        holder.btnInstantlyGrab.setTag(position);
        holder.rlfDesignPhoto.setTag(position);
        holder.llfItem.setOnClickListener(this);
        holder.btnInstantlyGrab.setOnClickListener(grabListener);
        holder.rlfDesignPhoto.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llfItem:
                if (!NoDoubleClickUtils.isDoubleClick(R.id.llfItem)) {
                    int pos = (int) v.getTag();
                    GrabOrderBean.DataBean b = list.get(pos);
                    Bundle bundle = new Bundle();
                    Intent i;
                    switch (b.getFtasktype()) {
                        case 0://待办任务（包括回访任务）
                            bundle.putInt(Tokens.OtherTask.OT_ITEMSKIPDETAIL_STATUS, -1);
                            bundle.putSerializable(Tokens.OtherTask.OT_ITEMSKIPDETAIL_ITEM, convertToOtherTaskbean(b));
                            i = new Intent(activity, OtherTaskDetailActivity.class);
                            i.putExtras(bundle);
                            activity.startActivity(i);
                            break;
                        case 1://测量任务
                            Intent intent1 = new Intent(activity, MeasureDetailActivity.class);
                            intent1.putExtra("isComplesh", false);
                            intent1.putExtra("measureid", b.getFtaskid());
                            intent1.putExtra("isGrabOrderBean", b);
                            activity.startActivity(intent1);
                            break;
                        case 5://送货任务
                            Intent intent5 = new Intent(activity, SendDetailActivity.class);
                            intent5.putExtra("isComplesh", false);
                            intent5.putExtra("dlvid", b.getFtaskid());
                            intent5.putExtra("isGrabOrderBean", b);
                            activity.startActivity(intent5);
                            break;
                        case 6://安装任务
                            Intent intent6 = new Intent(activity, BuildDetailActivity.class);
                            intent6.putExtra("isComplesh", false);
                            intent6.putExtra("iftestimony", false);
                            intent6.putExtra("dlvid", b.getFtaskid());
                            intent6.putExtra("isGrabOrderBean", b);
                            activity.startActivity(intent6);
                            break;
                    }
                }
                break;
        }
    }

    @Override
    public void notifyDataSetChanged(List<GrabOrderBean.DataBean> dataList) {
        this.list = dataList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvfTitle;//任务标题
        //TextView tvftypes;//任务类型
        TextView tvfName;//客户姓名和性别
        TextView tvfBuilding;//楼盘
        TextView tvfTiemTitle;//时间标题
        TextView tvfExecuteTime;//预约/执行时间
        TextView tvfWorkpoints;//工分
        LinearLayout llfItem;
        RelativeLayout rlfDesignPhoto;
        Button btnInstantlyGrab;

        public ViewHolder(View itemView) {
            super(itemView);
            this.tvfTitle = itemView.findViewById(R.id.tvfTitle);
            //this.tvftypes = itemView.findViewById(R.id.tvftypes);
            this.tvfName = itemView.findViewById(R.id.tvName);
            this.tvfBuilding = itemView.findViewById(R.id.tvfBuilding);
            this.tvfTiemTitle = itemView.findViewById(R.id.tvfTiemTitle);
            this.tvfExecuteTime = itemView.findViewById(R.id.tvfExecuteDate);
            this.tvfWorkpoints = itemView.findViewById(R.id.tvfWorkpoints);
            this.llfItem = itemView.findViewById(R.id.llfItem);
            this.rlfDesignPhoto = itemView.findViewById(R.id.rlfChangePhoto);
            this.btnInstantlyGrab = itemView.findViewById(R.id.btnInstantlyGrab);
        }

        public void setData(GrabOrderBean.DataBean gpBean) {
            tvfTitle.setText(gpBean.getFtitle());
            CommonUtils.setSex(tvfName, gpBean.getFcname(), gpBean.getFsexdesc());
            this.tvfBuilding.setText(gpBean.getFbuildname());
            this.tvfWorkpoints.setText(gpBean.getFpoint());
        }
    }

//    /**
//     * 转为量尺、复尺任务对象
//     *
//     * @param goBean
//     * @return
//     */
//    public LiangChiBean convertToLcbean(GrabOrderBean.DataBean goBean) {
//        if (goBean != null) {
//            Gson gson = new Gson();
//            LiangChiBean liangChiBean = gson.fromJson(gson.toJson(goBean), LiangChiBean.class);
//            liangChiBean.setStatus("1");
//            return liangChiBean;
//        } else {
//            return null;
//        }
//    }
//
//    /**
//     * 转为放样任务对象
//     */
//    public LoftingMainBean convertToFybean(GrabOrderBean.DataBean goBean) {
//        if (goBean != null) {
//            Gson gson = new Gson();
//            LoftingMainBean loftingMainBean = gson.fromJson(gson.toJson(goBean), LoftingMainBean.class);
//            loftingMainBean.setStatus("1");
//            return loftingMainBean;
//        } else {
//            return null;
//        }
//    }
//
//    /**
//     * 转为设计任务对象
//     */
//    public DesignItemBean convertToDesignbean(GrabOrderBean.DataBean goBean) {
//        if (goBean != null) {
//            Gson gson = new Gson();
//            return gson.fromJson(gson.toJson(goBean), DesignItemBean.class);
//        } else return null;
//    }

    /**
     * 转为其它对象
     */
    public OtherTaskMainBean.DataBean convertToOtherTaskbean(GrabOrderBean.DataBean goBean) {
        if (goBean != null) {
            Gson gson = new Gson();
            OtherTaskMainBean.DataBean result = gson.fromJson(gson.toJson(goBean), OtherTaskMainBean.DataBean.class);
            result.setFplantaskid(goBean.getFtaskid());
            return result;
        } else return null;
    }

    private void setViewByStatus(GrabOrderBean.DataBean gpBean, ViewHolder vh) {
        switch (gpBean.getFtasktype()) {
            //0.待办任务（包含回访任务），1.量尺，2.设计，3.复尺，4.放样，5.送货，6.安装
            case 0:
                vh.tvfTiemTitle.setText("执行时间：");
                vh.tvfExecuteTime.setText(gpBean.getFplantime()+ gpBean.getFplantime2());
                break;
            case 1://量尺
                vh.tvfTiemTitle.setText("预约测量时间：");
                vh.tvfExecuteTime.setText(gpBean.getFplantime()+ gpBean.getFplantime2());
                break;
            case 2://设计
                vh.tvfTiemTitle.setText("预约沟通时间：");
                vh.tvfExecuteTime.setText(gpBean.getFplantime()+ gpBean.getFplantime2());
                break;
            case 3://复尺
                vh.tvfTiemTitle.setText("预约复测时间：");
                vh.tvfExecuteTime.setText(gpBean.getFplantime()+ gpBean.getFplantime2());
                break;
            case 4://放样
                vh.tvfTiemTitle.setText("预约放样时间：");
                vh.tvfExecuteTime.setText(gpBean.getFplantime()+ gpBean.getFplantime2());
                break;
            case 5:
                vh.tvfTiemTitle.setText("预约送货时间：");
                vh.tvfExecuteTime.setText(gpBean.getFplantime()+ gpBean.getFplantime2());
                break;
            case 6:
                vh.tvfTiemTitle.setText("预约安装时间：");
                vh.tvfExecuteTime.setText(gpBean.getFplantime()+ gpBean.getFplantime2());
                break;
            default://其它任务
                vh.tvfTiemTitle.setText("执行时间：");
                vh.tvfExecuteTime.setText(gpBean.getFplantime()+ gpBean.getFplantime2());
                break;
        }
    }
}
